package com.bjpowernode.crm.settings.service.impl;

import com.bjpowernode.crm.exception.LoginException;
import com.bjpowernode.crm.settings.dao.UserDao;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.utile.Const;
import com.bjpowernode.crm.utile.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:UserServiceImpl
 * package:com.bjpowernode.crm.settings.service.impl
 * Description:
 *
 * @date: 2022-03-13 17:36
 * @author：lin
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    @Override
    public User login(String loginAct, String loginPwd, String ip) throws LoginException {

        Map<String,String> map =new HashMap<>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",loginPwd);

        User user=userDao.login(map);

        //判断user是否为空
        if (user==null){
            throw new LoginException("账号密码错误");
        }
        //账号密码正确
        String expireTime=user.getExpireTime();
        if (expireTime.compareTo(DateTimeUtil.getSysTime())<0){//compareTo
            throw new LoginException("账号已经失效");
        }

        //验证ip地址
        String allowIps=user.getAllowIps();
        if (allowIps!=null){

            if(!allowIps.contains(ip)){ //包含contains
                throw new LoginException("ip已经失效");

            }
        }


        //验证账号锁定状态
        String lockState=user.getLockState();
        if (lockState!=null){
            if (Const.LOCKSTATE_CLOSE.equals(lockState)){
                throw new LoginException("账号已经锁定");
            }
        }



        return user;
    }

    @Override
    public List<User> getUser() {
       List<User> uList  =userDao.getUser();

        return uList;
    }
}






















